//
//  SDUserReportViewController.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/31.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDUserReportViewController.h"
#import "SDUserReportTableViewCell.h"
#import "SDEmptyView.h"

#import "SDMyReportData.h"
#import "SDMyReportModel.h"

#import "SDDetectionReportVC.h"
#import "SDAlertController.h"

static NSString *const cellIden = @"SDUserReportTableViewCell";
@interface SDUserReportViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)SDEmptyView *emptyView;//当没有报告时显示的空白页

@property (nonatomic ,strong)SDMyReportModel *showModel;//显示数据

@property (nonatomic ,strong)NSMutableArray<SDMyReportData *> *myReportData;//数据集合
@end

@implementation SDUserReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _navHeight.constant = SDNavBar_Height;
    [self.tableView setTableFooterView:[UIView new]];
    [self.tableView registerNib:[UINib nibWithNibName:cellIden bundle:nil] forCellReuseIdentifier:cellIden];
    
    /*给空白页加约束*/
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.left.right.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-55);
        make.height.mas_equalTo(115);
    }];
    
    [self loadData];
    
}

 /**
  加载数据
  */
 - (void)loadData
{
    [SDNetwork getCustCheckReportListWithFinish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
        if (!error) {
            self.showModel = [SDMyReportModel modelObjectWithDictionary:dic];
            self.myReportData = [NSMutableArray arrayWithArray:self.showModel.data];
            self.emptyView.hidden = !(self.myReportData.count == 0);
            [self.tableView reloadData];
        }
    }];
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    SDUserReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
    cell.data = self.myReportData[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.myReportData.count;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    
        [self alertWithWithIndexPath:indexPath];
    }];
    [action setBackgroundColor:SDRedColor];
    return @[action];
    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //进入报告详情页面
    SDMyReportData *data = self.showModel.data[indexPath.row];
    SDDetectionReportVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDDetectionReportVC class])];
    vc.reportId = [data.id intValue];
    vc.isFromReportList = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
/**
 删除报告
 
 @param indexPath 要删除的报告的indexPath
 */
- (void)deleteReportWithIndexPath:(NSIndexPath *)indexPath
{
    SDMyReportData *data = self.myReportData[indexPath.row];
    [SDNetwork postDeleteReportWithParameter:NSSTRINGAPPEND(@"/0/", data.id) finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
        if (!error) {
            [self.myReportData removeObject:data];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView reloadData];
             self.emptyView.hidden = !(self.myReportData.count == 0);
        }
    }];
    
}
//空白页
-(SDEmptyView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [SDMethodTool getViewWithViewName:NSStringFromClass([SDEmptyView class])];
        _emptyView.hidden = YES;
        [self.view addSubview:_emptyView];
    }
    
    return _emptyView;
}

/**
 警告框

 @param indexPath 要删除的报告的indexPath
 */
- (void)alertWithWithIndexPath:(NSIndexPath *)indexPath
{
    SDAlertController *alert = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDAlertController class])];
    
    [alert.view setFrame:self.view.bounds];
    [self.view addSubview:alert.view];
    [self addChildViewController:alert];
    [alert setAlertContent:deleteReportAlertContent Title:deleteReportAlertTitle CancelTitle:deleteReportAlertCancel ConfirmTitle:deleteReportAlertConfirm];
    [alert.confirmBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        
        [alert dismissView];
        [self deleteReportWithIndexPath:indexPath];
        
    }];
    
}
- (IBAction)back:(UIButton *)sender {
    
     [self.navigationController popViewControllerAnimated:YES];
    
}
@end
